package com.txd.mofa.adapter;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kongzue.baseframework.BaseFragment;
import com.kongzue.baseframework.util.Preferences;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.MyApplication;
import com.txd.mofa.R;
import com.txd.mofa.view.MyJzvdStd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jzvd.Jzvd;

import static com.txd.mofa.http.HttpUtils.addFootprint;
import static com.txd.mofa.http.HttpUtils.updateAndroid;

public class TupianAdapter1 extends BaseQuickAdapter<Map<String, String>, BaseViewHolder> implements ViewPager.OnPageChangeListener {

    private MyJzvdStd myJzvdStd;
    private PageChange pageChange;
    private int pos = 0;
    private BaseViewHolder helper;
    private String s = "999";


    public TupianAdapter1(int layoutResId) {
        super(layoutResId);
    }

    public void setType(String s) {

        this.s = s;
    }

    private List<View> views;
    private View view1;
    private View view2;

    public void setPos(int pos) {
        this.pos = pos;
    }


    @Override
    protected void convert(BaseViewHolder helper, Map<String, String> item) {
        this.helper = helper;


        helper.addOnClickListener(R.id.iv_zhuanfa);

        ImageView iv_qie = helper.getView(R.id.iv_qiehuan);
        ImageView iv_sc = helper.getView(R.id.iv_sc);

        if (item.get("collect").equals("2")) {
            Glide.with(mContext).load(R.mipmap.xiangq_sc).into((ImageView) helper.getView(R.id.iv_sc));
        } else {
            Glide.with(mContext).load(R.mipmap.xiangq_sc_xuan).into((ImageView) helper.getView(R.id.iv_sc));
        }

        helper.setText(R.id.tv_name, item.get("name"));
        helper.setText(R.id.tv_lianxing, item.get("face"));
        helper.setText(R.id.tv_se, item.get("color"));
        helper.setText(R.id.tv_changhe, item.get("occasion"));

        Log.e("type1", s);
        if (s.equals("2")) {
            Glide.with(mContext).load(R.mipmap.xiangq_huan_xuan).into(iv_qie);
        } else {

            helper.addOnClickListener(R.id.iv_qiehuan);
            helper.addOnClickListener(R.id.iv_sc);

            if (item.get("qiehuan") != null) {
                if (item.get("qiehuan").equals("111")) {
                    Glide.with(mContext).load(R.mipmap.xiangq_huan_xuan).into(iv_qie);
                } else {
                    Glide.with(mContext).load(R.mipmap.xiangq_huan).into(iv_qie);
                }
            } else {
                Glide.with(mContext).load(R.mipmap.xiangq_huan).into(iv_qie);
            }
        }

        RecyclerView recy1 = helper.getView(R.id.recy1);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(mContext);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        recy1.setLayoutManager(linearLayoutManager1);

        BiaoqianAdapter biaoqianAdapter = new BiaoqianAdapter(R.layout.item_biaoqian);
        recy1.setAdapter(biaoqianAdapter);

        ArrayList<Map<String, String>> label = JSONUtils.parseKeyAndValueToMapList(item.get("label"));


        biaoqianAdapter.setNewData(label);


        if (!item.get("video").equals("")) {

            ViewPager viewpager = helper.getView(R.id.viewPager);

            views = new ArrayList<View>();
            view1 = LayoutInflater.from(mContext).inflate(R.layout.item_page2, null);
            view2 = LayoutInflater.from(mContext).inflate(R.layout.item_tupian, null);
            views.add(view1);
            views.add(view2);

            MyPageAdapter adapter = new MyPageAdapter(views);
            viewpager.setAdapter(adapter);
            viewpager.setOnPageChangeListener(this);
            viewpager.setCurrentItem(pos);

            pageChange.change(pos, "1");

            myJzvdStd = view1.findViewById(R.id.mp_video);

            String proxyUrl = MyApplication.getProxy(mContext.getApplicationContext()).getProxyUrl(item.get("video"));

            myJzvdStd.setUp(proxyUrl, "");
            if (pos == 0) {
                myJzvdStd.startVideo();
            }
            Glide.with(mContext).load(item.get("video_cover")).into(myJzvdStd.thumbImageView);
            myJzvdStd.thumbImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        } else {

            ViewPager viewpager = helper.getView(R.id.viewPager);

            views = new ArrayList<View>();
            view2 = LayoutInflater.from(mContext).inflate(R.layout.item_tupian, null);
            views.add(view2);

            MyPageAdapter adapter = new MyPageAdapter(views);
            viewpager.setAdapter(adapter);
            viewpager.setOnPageChangeListener(this);
            viewpager.setCurrentItem(1);
            pageChange.change(1, "0");

        }

        ImageView iv = view2.findViewById(R.id.iv);

        Glide.with(mContext).load(item.get("img")).into(iv);

    }

    public void setPageChange(PageChange pageChange) {

        this.pageChange = pageChange;
    }

    public interface PageChange {
        void change(int pos, String s);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        pageChange.change(position, "1");

        if (position == 0) {
            myJzvdStd.startVideo();
        } else if (position == 1) {
            Jzvd.releaseAllVideos();
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class MyPageAdapter extends PagerAdapter {
        private List<View> views;

        public MyPageAdapter(List<View> views) {

            this.views = views;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ViewGroup group = (ViewGroup) views.get(position).getParent();
            if (group != null) {
                group.removeView(views.get(position));
            }
            ((ViewPager) container).addView(views.get(position));
            return views.get(position);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(views.get(position));
        }
    }
}
